package com.wanxi.dangdang.mapper;

import com.wanxi.dangdang.model.vo.PermissionFindPageVO;
import com.wanxi.dangdang.model.pojo.Permission;
import com.wanxi.dangdang.model.vo.PermissionVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: 吕永杰
 * @Description:
 * @Date: Created in 2021/8/31
 * @Version: 1.0
 */
@Repository
public interface PermissionMapper {

    //检验是否纯在
    @Select("select name from permission where name=#{name}")
    String checkName(String name);

    @Select(
            "        SELECT distinct p.* FROM\n" +
                    "        admin_role_relation ar JOIN role_permission_relation rp on ar.role_id = rp.role_id\n" +
                    "        JOIN permission p on rp.permission_id = p.id\n" +
                    "        WHERE ar.admin_id=#{id} and p.delete_status=0"
    )
    List<Permission> getPermissionsByUserId(Long id);

    @Insert("insert into permission (name,description,uri,create_time,update_time) value (#{name},#{description},#{uri},NOW(),NOW())")
    void add(Permission permission);

    @Select("select id,name,description,uri from permission where id=#{id}")
    Permission findById(Long id);

    @Select("select id,name,description,uri from permission where delete_status=0")
    List<PermissionFindPageVO> findPage(PermissionVo permissionVo);

    @Update("update permission set delete_status=1,update_time=NOW() where id=#{id}")
    void deleteById(Long id);

    //删除关于role_permission_relation中间表的数据
    @Delete("Delete from role_permission_relation where permission_id=#{id}")
    void deleteRolePermissionById(Long id);

    @Update("update permission set name=#{name},description=#{description},uri=#{uri},update_time=NOW() where id=#{id}")
    void updateById(Permission permission);

    @Select("select p.id,p.name,p.description,p.uri from permission p,role_permission_relation rp where p.id=rp.permission_id and rp.role_id=#{id}")
    List<PermissionFindPageVO> findPermissionsByRoleId(Long id);
}
